package com.woniuxy.dr_mall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.dr_mall.entity.*;
import com.woniuxy.dr_mall.mapper.DrCartMapper;
import com.woniuxy.dr_mall.mapper.DrOrdersItemMapper;
import com.woniuxy.dr_mall.mapper.DrOrdersMapper;
import com.woniuxy.dr_mall.mapper.DrSpecPriceMapper;
import com.woniuxy.dr_mall.service.IDrOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@Service
@Transactional
public class DrOrdersServiceImpl extends ServiceImpl<DrOrdersMapper, DrOrders> implements IDrOrdersService {
    @Autowired
    private DrOrdersMapper ordersMapper;
    @Autowired
    private DrOrdersItemMapper ordersItemMapper;

    @Autowired
    private DrSpecPriceMapper specPriceMapper;

    @Autowired
    private DrCartMapper cartMapper;

    @Override
    public List<DrProducts> getOrdersByUserId(int userId) {
        return ordersMapper.getOrdersByUserId(userId);
    }

    @Override
    public DrCart getCartsById(int id) {
        return cartMapper.getCartsById(id);
    }

    @Override
    public DrOrders add(List<DrCart> carts, DrAddress addressMsg, int userId, Integer skuId) {
        DrOrders order = new DrOrders();
        DrOrdersItem ordersItem = new DrOrdersItem();
        StringBuilder ads = new StringBuilder(addressMsg.getProvince());
        ads.append(addressMsg.getCity()).append(addressMsg.getDistrict()).append(addressMsg.getAddress());
        //存储用户信息
        order.setUserId(userId);
        //存储收货地址
        order.setAddress(ads.toString());
        //存储支付方式
        order.setPayType("0");
        //存储支付时间
        order.setPayTime("");
        //存储收货人
        order.setAccept(addressMsg.getReceiver());
        //存储电话
        order.setTelephone(addressMsg.getTelephone());
        //存储订单号
        String OrdersNo = getOrdersNo();
        order.setOrderNo(OrdersNo);
        //存储下单时间
        order.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //存储金额
        BigDecimal totalMoney = getTotalMoney(carts, skuId);
        order.setMoney(totalMoney);
        //存储订单状态为未付款
        order.setStatus("1");
        //删除标识为不删除
        order.setIsDel("n");
        //往数据库里加
        ordersMapper.insert(order);
        //往订单详情里加数据
        if (carts.size()!=0){
            for (DrCart cart : carts) {
                ordersItem.setSkuId(cart.getSkus().getId());
                ordersItem.setOrdersId(order.getId());
                ordersItem.setStatus("y");
                ordersItemMapper.insert(ordersItem);
            }
        }else {
            ordersItem.setSkuId(specPriceMapper.getSkusBySkuId(skuId).getId());
            ordersItem.setOrdersId(order.getId());
            ordersItem.setStatus("n");
            ordersItemMapper.insert(ordersItem);
        }
        return order;
    }

    @Override
    public List<DrOrders> getAllOrders() {
        return ordersMapper.getAll();
    }

    private BigDecimal getTotalMoney(List<DrCart> carts, Integer skuId) {
        //总价格
        BigDecimal totalMoney = new BigDecimal("0");
        if (carts.size() != 0) {
            for (DrCart cart : carts) {
                totalMoney = totalMoney.add(cart.getPrice());
            }
        } else {
            DrSpecPrice specPrice = specPriceMapper.getSkusBySkuId(skuId);
            totalMoney=totalMoney.add(specPrice.getPrice());
        }
        return totalMoney;
    }

    private String getOrdersNo() {
        String prefix = "DR";
        String nowTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        Random random = new Random();
        int suffix = random.nextInt(9000) + 1000;
        String OrderNo = prefix + nowTime + suffix;
        return OrderNo;
    }

    //通过用户id，查询用户的订单信息 2021-8-16 叶官鑫
    //
    @Override
    public List<DrOrders> userIdSelectOrders(int userId) {
        //默认查询以存在的id
        //默认加一个status，作为查询条件 2021-8-17
        String status="y";
        return ordersMapper.userIdSelectOrders(userId,status);
    }

    //通过用户id，和订单id 对订单进行逻辑删除 叶官鑫 2021-8-17
    @Override
    public void idDeleteOrder(int userId, int orderId) {
        ordersMapper.idDeleteOrder(userId,orderId);
    }
    //通过订单id查询订单信息，以及用户的信息 叶官鑫 2021-8-17
    @Override
    public DrOrders selectOrderByid(int orderId) {
        return ordersMapper.selectOrderByid(orderId);
    }

     //通过订单id查询商品详情信息，以及规格信息 叶官鑫 2021-8-17
    @Override
    public List<DrOrderItemView> selectProductsImformationById(int orderId) {
        return ordersMapper.selectProductsImformationById(orderId);
    }

    @Override
    public int getCountByUserId(int userId) {
        return ordersMapper.getCountByUserId(userId);

    }

    @Override
    public void forbidById(int userId) {
        ordersMapper.forbidById(userId);
    }
}
